public class MyLinkList implements IList{

    static class LinkList{
        public int val;
        public LinkList prev;
        public LinkList next;

        public LinkList(int val) {
            this.val = val;
        }
    }

    public LinkList head;
    public LinkList last;
    @Override
    public void addFirst(int data) {
        LinkList cur=new LinkList(data);
        if(head==null){
            head=last=null;
        }else{
            head.prev=cur;
            cur.next=head;
            head=cur;
        }
    }

    @Override
    public void addLast(int data) {
        LinkList cur=new LinkList(data);
        if(head==null){
            head=last=null;
        }else{
            last.next=cur;
            cur.prev=last;
            last=last.next;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||len<index){
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        LinkList cur=head;
        LinkList node=new LinkList(data);
        int i=0;
        while (cur!=null){
            if(i==index){
                node.next=cur;
                node.prev=cur.prev;
                cur.prev.next=node;
                cur.prev=node;
            }
        }
    }



    @Override
    public boolean contains(int key) {
        LinkList cur=head;
        while (cur !=null){
            if(cur.val==key){
                return true;
            }
        }
        return false;
    }

    //删除指定位置,需要考虑删除头结点和尾节点
    @Override
    public void remove(int key) {
        LinkList cur=head;

        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){  //考虑如果只有一个节点
                        head.prev=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur.next==null){
                        last=last.prev;
                    }else{
                        cur.next.prev=cur.prev;
                    }
                }
                return;
            }
                cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        LinkList cur=head;

        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){  //考虑如果只有一个节点
                        head.prev=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur.next==null){
                        last=last.prev;
                    }else{
                        cur.next.prev=cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public int size() {
        LinkList cur=head;
        int len=0;
        while(cur !=null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        LinkList cur=head;
        while(cur!=null){
            LinkList curN=cur.next;
            cur.prev=null;
            cur.next=null;
            cur=curN.prev;
            
        }
    }

    @Override
    public void display() {
        LinkList cur=head;
        while(cur !=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println("");
    }
}
